package com.junzhiit.nengji.shop.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.shop.bo.GoodsBO;
import com.junzhiit.nengji.shop.dao.FavoriteDAO;
import com.junzhiit.nengji.shop.model.Favorite;
import com.junzhiit.nengji.shop.model.enums.GoodsImagesType;
import com.junzhiit.nengji.shop.model.enums.GoodsStatus;

@Repository
public class FavoriteDAOImpl extends AbstractBaseDAO<Favorite> implements FavoriteDAO {

	public Favorite getFavorite(String userId,String goodsId) {
		StatementParameter params = new StatementParameter();
		StringBuilder sql = new StringBuilder();
		sql.append("select * from favorite where user_id = ? and goods_id = ? and is_delete =?");
		params.setString(userId);
		params.setString(goodsId);
		params.setInt(IsDelete.NO.toIntValue());
		return super.query(sql.toString(), Favorite.class, params);
	}

	public int getFavoriteNumber(String userId,String goodsId){
		StatementParameter params = new StatementParameter();
		StringBuilder sql = new StringBuilder();
		sql.append("select count(*) from favorite where user_id in ("+userId+") and goods_id = ? and is_delete =?");
		return super.queryForInt(sql.toString(), params);
	}
	
	public List<GoodsBO> getFavoriteList(Integer pageNumber,Integer maxPageRow,String userId) {
		StringBuilder sql = new StringBuilder();
		StatementParameter params = new StatementParameter();
		sql.append("select f.id 'favoriteId',g.id 'goods_id',g.code,g.name,g.price,g.reference_price,");
		sql.append("b.name 'brandName',b.logo_img_url 'brandLogoImgUrl',gm.file_name 'cover_image',g.shop_id,g.type,");
		sql.append("g.is_open_sku,g.is_open_member,(select min(price) from sku_content where goods_id=g.id) 'sku_price'");
		sql.append(" from favorite f inner join goods g on g.id = f.goods_id");
		sql.append(" inner join shop s on s.id = g.shop_id");
		sql.append(" inner join brand b on b.id = g.brand_id");
		sql.append(" left join goods_images gm on gm.goods_id = g.id ");
		sql.append(" and gm.type = ? and gm.is_delete = ?");
		sql.append(" where g.status = ? and g.is_delete = ? and f.is_delete =? and f.user_id = ?");
		sql.append(" order by f.created_time desc");
		params.setInt(GoodsImagesType.COVER.toIntStatus());
		params.setInt(IsDelete.NO.toIntValue());
		params.setInt(GoodsStatus.ACTIVE.toIntStatus());
		params.setInt(IsDelete.NO.toIntValue());
		params.setInt(IsDelete.NO.toIntValue());
		params.setString(userId);
		return super.queryForPagingList(sql.toString(),GoodsBO.class, params, pageNumber, maxPageRow);
	}

	public int getFavoriteTotalCount(String userId) {
		StringBuilder sql = new StringBuilder();
		StatementParameter params = new StatementParameter();
		sql.append("select count(f.id)");
		sql.append(" from favorite f inner join goods g on g.id = f.goods_id");
		sql.append(" inner join shop s on s.id = g.shop_id");
		sql.append(" inner join brand b on b.id = g.brand_id");
		sql.append(" left join goods_images gm on gm.goods_id = g.id ");
		sql.append(" and gm.type = ? and gm.is_delete = ?");
		sql.append(" where g.status = ? and g.is_delete = ? and f.is_delete =? and f.user_id = ?");
		sql.append(" order by f.created_time desc");
		params.setInt(GoodsImagesType.COVER.toIntStatus());
		params.setInt(IsDelete.NO.toIntValue());
		params.setInt(GoodsStatus.ACTIVE.toIntStatus());
		params.setInt(IsDelete.NO.toIntValue());
		params.setInt(IsDelete.NO.toIntValue());
		params.setString(userId);
		return super.queryForInt(sql.toString(), params);
	}

	public int totalCountByUserId(String userId) {
		String sql = "select count(id) from favorite where user_id = ?";
		StatementParameter params = new StatementParameter();
		params.setString(userId);
		return super.queryForInt(sql, params);
	}

	@Override
	public int deleteByGoodsAndUserId(String[] goosd, String userId) {
		// TODO Auto-generated method stub
		StringBuilder sql = new StringBuilder();
		StatementParameter params = new StatementParameter();
		sql.append("delete from favorite where user_id = ? and goods_id in(");
		params.setString(userId);
		for (int i= 0;i<goosd.length;i++) {
			if(i == 0){
				sql.append("?");
			}else{
				sql.append(",?");
			}
			params.setString(goosd[i]);
		}
		sql.append(")");
		return super.merge(sql.toString(), params);
	}
	
}
